package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.entity.TService;
import com.woniu.car.exception.ParamException;
import com.woniu.car.mapper.TServiceMapper;
import com.woniu.car.service.TServiceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author *********
 * @since 2021-06-21
 */
@Service
public class TServiceServiceImpl extends ServiceImpl<TServiceMapper, TService> implements TServiceService {

    /**
    *@Author 李磊
    *@Description 新增服务
    *@Param [service]
    *@return void
    **/
    @Override
    @Transactional
    public TService insert(TService service){

        String serviceName = service.getServiceName();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("service_name",serviceName);
        List<TService> tServices = this.list(wrapper);

        for (TService tService : tServices) {
            if(null != tServices && service.getShopId() == tService.getShopId()){
                throw new ParamException("该服务已经存在");
            }
        }

        String code = UUID.randomUUID().toString();
        service.setServiceNo(code);

        this.save(service);

        TService tService = this.getById(service.getServiceId());

        return tService;

    }
}
